package com.linkwechat.service.impl;

import com.linkwechat.common.component.CacheService;
import com.linkwechat.common.utils.ParamCheckUtil;
import com.linkwechat.common.utils.RandomUtil;
import com.linkwechat.common.utils.SHA1Util;
import com.linkwechat.common.vo.qw.QYWXAppJSAPITicketVO;
import com.linkwechat.common.vo.qw.QYWXQYJSAPITicketVO;
import com.linkwechat.domain.wecom.vo.ConfigSignatureVO;
import com.linkwechat.domain.wecom.vo.user.QwDeptUserVO;
import com.linkwechat.util.QYWXUtil;
import com.linkwechat.common.vo.qw.AccessTokenVO;
import com.linkwechat.common.vo.qw.GetVisitUserVO;
import com.linkwechat.domain.wecom.vo.department.QwDeptListVO;
import com.linkwechat.service.QwService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author 王君平
 * @Date 2024/6/8 16:34
 * @Description
 */
@Service
public class QwServiceImpl implements QwService {

    @Autowired
    private CacheService cacheService;

    @Override
    public String accessToken(String corpId, String corpSecret) {
        AccessTokenVO vo = cacheService.getAccessTokenFromCache();
        if(vo != null){
            return vo.getAccess_token();
        }
        vo = QYWXUtil.getAccessToken(corpId,corpSecret);
        cacheService.setAccessTokenToCache(vo);
        return vo.getAccess_token();
    }

    @Override
    public String contactAccessToken(String corpId, String contactSecret){
        AccessTokenVO vo = cacheService.getContactAccessTokenFromCache(corpId);
        if(vo != null){
            return vo.getAccess_token();
        }
        vo = QYWXUtil.getAccessToken(corpId,contactSecret);
        cacheService.setContactAccessTokenToCache(corpId,vo);
        return vo.getAccess_token();
    }

    @Override
    public GetVisitUserVO getLoginAuthVisitUser(String code, String accessToken) {
        ParamCheckUtil.stringEmpty(code,"code不能为空");
        ParamCheckUtil.stringEmpty(accessToken,"accessToken不能为空");
        GetVisitUserVO vo = QYWXUtil.getVisitUserForSelfApp(accessToken,code);
        return vo;
    }

    @Override
    public QwDeptListVO deptList(String accessToken, String id) {
        ParamCheckUtil.stringEmpty(accessToken,"accessToken不能为空");
        QwDeptListVO vo = QYWXUtil.getDeptList(accessToken,id);
        return vo;
    }

    @Override
    public QwDeptUserVO deptUser(String contactAccessToken, String nextCursor) {
        ParamCheckUtil.stringEmpty(contactAccessToken,"contactAccessToken不能为空");
        QwDeptUserVO vo = QYWXUtil.getDeptUser(contactAccessToken,nextCursor);
        return vo;
    }

    @Override
    public String getJsapiTicket(String accessToken) {
        QYWXQYJSAPITicketVO vo = cacheService.getQYWXJSAPITicketFromCache();
        if(vo != null){
            return vo.getTicket();
        }
        ParamCheckUtil.stringEmpty(accessToken,"accessToken不能为空");
        vo = QYWXUtil.getQYWXQYJSAPITicket(accessToken);
        cacheService.setQYWXJSAPITicketToCache(vo);
        return vo.getTicket();
    }

    @Override
    public ConfigSignatureVO getConfigSignature(String jsapiTicket, String url) {
        ParamCheckUtil.stringEmpty(jsapiTicket,"企业的jssdkTicket不能为空");
        String noncestr = RandomUtil.getRandomString(16);
        Long timestamp = new Date().getTime() /1000;
        ConfigSignatureVO vo = new ConfigSignatureVO();
        vo.setNonceStr(noncestr);
        vo.setTimestamp(timestamp);
        //签名
        String str = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + noncestr + "&timestamp=" + String.valueOf(timestamp) + "&url=" + url;
        String signature = SHA1Util.encode(str);
        vo.setSignature(signature);
        return vo;
    }

    @Override
    public String getAgentJsapiTicket(String accessToken,String corpId) {
        QYWXAppJSAPITicketVO vo = cacheService.getQYWXAuthCorpJSAPITicketFromCache(corpId);
        if(vo != null){
            return vo.getTicket();
        }
        ParamCheckUtil.stringEmpty(accessToken,"accessToken不能为空");
        vo = QYWXUtil.getQYWXAppJSAPITicket(accessToken);
        cacheService.setQYWXAuthCorpJSAPITicketToCache(corpId,vo);
        return vo.getTicket();
    }

    @Override
    public ConfigSignatureVO getAgentConfigSignature(String jsapiTicket, String url) {
        ParamCheckUtil.stringEmpty(jsapiTicket,"应用的jssdkTicket不能为空");
        String noncestr = RandomUtil.getRandomString(16);
        Long timestamp = new Date().getTime() /1000;
        ConfigSignatureVO vo = new ConfigSignatureVO();
        vo.setNonceStr(noncestr);
        vo.setTimestamp(timestamp);
        //签名
        String str = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + noncestr + "&timestamp=" + String.valueOf(timestamp) + "&url=" + url;
        String signature = SHA1Util.encode(str);
        vo.setSignature(signature);
        return vo;
    }

}
